﻿//_______________________________________________________________________________________________________________________
// Copy Right (c) 2010, 2011 SapiensIT Consulting GmbH, all rights reserved
//_______________________________________________________________________________________________________________________
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//_______________________________________________________________________________________________________________________
// PROJECT:     sapiens.at.SharePoint (SharePoint 2010/V4)
//_______________________________________________________________________________________________________________________
// VERSION:     2.0
//_______________________________________________________________________________________________________________________
// DESCRIPTION: 
//_______________________________________________________________________________________________________________________
// LICENSE:     Different licens Types apply to different file types:
//               - Source code files are governed by the MICROSOFT PUBLIC LICENSE (Ms-PL)
//               - Binary files are governed by MSDN CODE GALLERY BINARY LICENSE
//               - Documentation files are governed by CREATIVE COMMONS ATTRIBUTION 3.0 LICENSE
//
//               The licenses can be found at the projects source location.
//               If you do not agree, don't use it!
// 
//_______________________________________________________________________________________________________________________
// Start SharePoint Branding, visit http://www.cliggin.com! Visit us on http://www.sapiens.at! Tell your friends! 
//_______________________________________________________________________________________________________________________
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;
using System.Web.UI;

namespace Sapiens.at.SharePoint.Controls
{
    /// <summary>
    /// Extends the standard SharePoint listfielditerator.
    /// </summary>
    public class ListFieldIteratorEx : ListFieldIterator
    {
        /// <summary>
        /// Tries to find a control with a certain id.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public override Control FindControl(string id)
        {
            var field = Fields.GetFieldByInternalName(id);
            if (field != null)
            {
                return field.FieldRenderingControl;
            }
            return base.FindControl(id);
        }

        /// <summary>
        /// Adds a field to the collection of excluded fields.
        /// </summary>
        /// <param name="fieldName"></param>
        public void AddExcludeField(string fieldName)
        {
            if (string.IsNullOrEmpty(ExcludeFields)) { ExcludeFields = fieldName; }
            else if (!ExcludeFields.Split(new string[] { ";#" }, StringSplitOptions.RemoveEmptyEntries).Contains(fieldName))
            {
                ExcludeFields += ";#" + fieldName;
            }
        }

        /// <summary>
        /// Adds fields to the collection of excluded fields.
        /// </summary>
        /// <param name="fieldNames"></param>
        public void AddExcludeField(string[] fieldNames)
        {
            for (var i = 0; i < fieldNames.Length; i++)
            {
                AddExcludeField(fieldNames[i]);
            }
        }

        /// <summary>
        /// Adds a field to the collection of excluded fields.
        /// </summary>
        /// <param name="field"></param>
        public void AddExcludeField(SPField field)
        {
            AddExcludeField(field.InternalName);
        }

        /// <summary>
        /// Returns the BaseFieldControl for the given field name.
        /// </summary>
        /// <param name="internalName"></param>
        /// <returns></returns>
        public BaseFieldControl GetFieldControl(string internalName)
        {
            return ItemContext.FormContext.FieldControlCollection.Cast<BaseFieldControl>().FirstOrDefault(fc => fc.FieldName == internalName);
        }

        /// <summary>
        /// Returns a field control.
        /// </summary>
        /// <typeparam name="TControl">The type of the resulting control</typeparam>
        /// <param name="internalName">The internal name of the field</param>
        /// <returns></returns>
        public TControl GetControl<TControl>(string internalName)
            where TControl : System.Web.UI.Control
        {
            Control bfc = GetFieldControl(internalName);
            return GetControl<TControl>(bfc);
        }

        /// <summary>
        /// Returns a field control.
        /// </summary>
        /// <typeparam name="TControl">The type of the resulting control</typeparam>
        /// <param name="bfc">The control that contains the searched fieldcontrol</param>
        /// <returns></returns>
        protected TControl GetControl<TControl>(Control bfc)
            where TControl : System.Web.UI.Control
        {
            if (bfc == null) return null;
            if (bfc is TControl) { return (TControl)bfc; }
            TControl control;
            foreach (Control c in bfc.Controls)
            {
                if (c.GetType() == typeof(TControl)) { return (TControl)c; }
                control = GetControl<TControl>(c);
                if (control != null) return control;
            }
            return null;
        }

        /// <summary>
        /// Enables or disables a certain control.
        /// </summary>
        /// <typeparam name="TControl">The type of control to enable/disable</typeparam>
        /// <param name="internalName">The internal name of the field</param>
        /// <param name="value"></param>
        public void SetControlEnabled<TControl>(string internalName ,bool value)
            where TControl : System.Web.UI.Control
        {
            var control = GetControl<TControl>(internalName);
            control.GetType().InvokeMember("Enabled", System.Reflection.BindingFlags.SetProperty, null, control, new object[] { value });
        }

        /// <summary>
        /// Hides or shows a certain field control.
        /// </summary>
        /// <param name="value">true, to hide; otherwise false</param>
        /// <param name="internalNames">The internal name of the field</param>
        public void HideFields(bool value, params string[] internalNames)
        {
            foreach (var name in internalNames)
            {
                BaseFieldControl bfc = GetFieldControl(name);
                bfc.Visible = !value;
                if (bfc.Parent != null)
                {
                    bfc.Parent.Visible = !value;
                    if (bfc.Parent.Parent != null)
                    { bfc.Parent.Parent.Visible = !value; }
                }
            }
        }
    }
}
